package com.t114514.demo.service.impl;

import com.t114514.demo.mapper.CourseMapper;
import com.t114514.demo.pojo.Course;
import com.t114514.demo.pojo.PageBean;
import com.t114514.demo.pojo.Result;
import com.t114514.demo.service.CourseService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Override
    public PageBean<Course> getCourse(int pageNum, int pageSize) {
        // 初始化PageBean对象
        PageBean<Course> pb = new PageBean<>();
        // 启动分页功能，指定当前页码和页面大小
        PageHelper.startPage(pageNum, pageSize);
        // 执行课程查询，由于使用了分页助手，这里会自动进行分页查询
        List<Course> as = courseMapper.selectCourse();
        // 将查询结果转换为Page对象，以便获取分页信息
        Page<Course> p = (Page<Course>) as;
        // 设置总记录数到PageBean对象
        pb.setTotal(p.getTotal());
        // 设置课程列表到PageBean对象
        pb.setItems(p.getResult());
        // 返回填充了分页信息和课程列表的PageBean对象
        return pb;
    }

    @Override
    public void addCourse(Course course) {
        courseMapper.insertCourse(course);
    }

    @Override
    public void deleteCourse(Integer courseId) {
        courseMapper.deleteCourse(courseId);
    }
}
